<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
  <!-- 状态列 -->
  <template #status="{ record }">
          <a-switch
            :checked-value="1"
            unchecked-value="2"
            @change="changeStatus($event, record.id)"
            :default-checked="record.status == 1" 
          />
        </template>
      <template #moudel_icon="{ record }">
 
            <a-image :src="record.moudel_icon ? $tool.showFile(record.moudel_icon) : $url + 'avatar.jpg'" style="object-fit: cover"  width="40"/>
    
      </template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import systemDeviceOrder from '@/api/system/systemDeviceOrder'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'
const crudRef = ref()
const props = defineProps(["client", "curDevieInfo", "case_id","isDelete"]);
const numberOperation = (newValue, id, numberName) => {
  systemDeviceOrder.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const changeStatus = async (value, id) => {
  systemDeviceOrder.changeStatus({ id:id,statusValue:value}).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
  }

const options = reactive({
  beforeRequest: params => {
    // 指定排序字段为浏览量字段
    params.device_id = props.curDevieInfo.id

  },
  id: 'system_device_order',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: systemDeviceOrder.getList,
  recycleApi: systemDeviceOrder.getRecycleList,
  add: {
    show: true,
    api: systemDeviceOrder.save,
    auth: ['system:deviceOrder:save']
  },
  edit: {
    show: true,
    api: systemDeviceOrder.update,
    auth: ['system:deviceOrder:update']
  },
  delete: {
    show: true,
    api: systemDeviceOrder.deletes,
    auth: ['system:deviceOrder:delete'],
    realApi: systemDeviceOrder.realDeletes,
    realAuth: ['system:deviceOrder:realDeletes']
  },
  recovery: {
    show: true,
    api: systemDeviceOrder.recoverys,
    auth: ['system:deviceOrder:recovery']
  },
  import: {
    show: true,
    url: 'system/deviceOrder/import',
    templateUrl: 'system/deviceOrder/downloadTemplate',
    auth: ['system:deviceOrder:import']
  },
  export: {
    show: true,
    url: 'system/deviceOrder/export',
    auth: ['system:deviceOrder:export']
  }
})

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  {
    title: "模块名称",
    dataIndex: "moudel_name",
    formType: "input",
    search: true
  },
  {
    title: "模块图标",
    dataIndex: "moudel_icon",
    formType: "upload"
  },
  {
    title: "指令名称",
    dataIndex: "order_name",
    formType: "input",
    search: true
  },
  {
    title: "设备编号",
    dataIndex: "device_num",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "指令代码",
    dataIndex: "order_code",
    formType: "textarea"
  },
  {
    title: "指令参数",
    dataIndex: "order_params",
    formType: "code-editor",
    height:100
  },
  {
    title: "触发条件",
    dataIndex: "order_where",
    formType: "radio",
    dict: {
      name: "trigger_type",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
     
    },
    addDefaultValue: 1,
    labelWidth: '86px'
  },
  {
    title: "所属机器人",
    dataIndex: "device_id",
    formType: "input",
    addDisplay: true,
    editDisplay: true,
    addDefaultValue: props.curDevieInfo.id,
    editDefaultValue: props.curDevieInfo.id,
    hide: true,
    sortable: {
      sortDirections: [
        "ascend",
        "descend"
      ],
      sorter: true
    }
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "所属区域",
    dataIndex: "dept_id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "显示状态",
    dataIndex: "status",
    formType: "radio",
    search: true,
    dict: {
      name: "data_status",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    },
     addDefaultValue: '1',
      labelWidth: '86px'
  },
  {
    title: "备注信息",
    dataIndex: "remark",
    formType: "textarea",
    addDisplay: true,
    editDisplay: true
  }
])
</script>
<script> export default { name: 'system:deviceOrder' } </script>